package com.ruoyi.project.edu.course.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.edu.stuCourse.domain.StuCourse;
import com.ruoyi.project.edu.stuCourse.service.IStuCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.edu.course.domain.Course;
import com.ruoyi.project.edu.course.service.ICourseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author yueqiangu
 * @date 2021-02-27
 */
@Controller
@RequestMapping("/edu/course")
public class CourseController extends BaseController
{
    private String prefix = "edu/course";

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IStuCourseService stuCourseService;

    @RequiresPermissions("edu:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }

    @GetMapping("/search")
    public String search()
    {
        return prefix + "/search";
    }

//    @RequiresPermissions("edu:course:play")
    @GetMapping("/play")
    public String play(Long id,String src, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        mmap.put("storagePath", src);
        mmap.put("upAttList", course.getUpAttList());
        return prefix + "/play";
    }

//    @RequiresPermissions("edu:course:video")
    @GetMapping("/video")
    public String video(Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        mmap.put("upAttList", course.getUpAttList());
        return prefix + "/video";
    }

    /**
     * 表单
     */
    @GetMapping("/form")
    public String form(Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);

        StuCourse stuCourse = new StuCourse();
        stuCourse.setCourseId(id);
        stuCourse.setUserName(ShiroUtils.getLoginName());
        List<StuCourse> stuCourseList =stuCourseService.selectStuCourseList(stuCourse);
        if(StringUtils.isNotEmpty(stuCourseList)){
            mmap.put("flag", 1);
        }else{
            mmap.put("flag", 0);
        }
        mmap.put("course", course);
        return prefix + "/form";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("edu:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        TableDataInfo tableDataInfo = getDataTable(list);
        Long t = tableDataInfo.getTotal();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Integer total = Integer.parseInt(Long.toString(tableDataInfo.getTotal()));
        int totalPage = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
        List<Integer> pageArray = new ArrayList<>();
        for(int i =1 ;i<totalPage+1;i++){
            pageArray.add(i);
        }
        tableDataInfo.setPageArray(pageArray);
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        return tableDataInfo;
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("edu:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("edu:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("edu:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("edu:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}
